package com.github.llyb120.flow

class All {

    private static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList();
        }
    };


    static Flow flow(Closure closure){
        Flow flow = new Flow();
        pushStack(flow);
        closure.call();
        return flow;
    }

    /** node start **/
    static void node(Closure closure){
        //node必须在flow之下
        def flow = pickStack();//local.get().getLast();
        assert flow instanceof Flow;
        Node node = new Node();
        flow.getNodeList().add(node);
        pushStack(node);
        closure.call();
        popStack();
    }
    static void who(String name){

    }

    static void to(String name){

    }
    static void to(Closure closure){

    }
    static void which(String name){

    }

    /** node end **/

    /** actor start **/
    static void actor(Closure closure){
        def flow = pickStack();
        assert flow instanceof Flow;
        Actor actor = new Actor();
        flow.getActorList().add(actor);
        pushStack(actor);
        closure.call();
        popStack();
    }
    static void define(String name){
        def item = pickStack();
        if(item instanceof Actor){
            item.setDefineExpression(name);
            return
        }

        assert false
    }

    static void define(Closure closure){
        def item = pickStack();
        assert item instanceof Actor;
        item.setDefineFunction(closure);
    }

    /** actor end **/

    static void target(String nodeName){
        def node = pickStack();
        assert node instanceof Node;
        node.getTargetList().add(nodeName);
    }

    static void target(List targetList){
        def node = pickStack();
        assert node instanceof Node;
        node.getTargetList().addAll(targetList);
    }

    /** common start **/

    static void name(String name){
        def context = pickStack();
        if(context instanceof Node){
             context.setName(name);
        } else if(context instanceof Flow){

        } else if(context instanceof Actor){
            context.setName(name);
        } else {
            assert false;
        }
    }
    static void byName(String... names){

    }
    static void byId(String ids){

    }

    static void dynamic(Closure closure){
    }

    /** common end **/


    /** stack start **/
    private static Object pickStack(){
        return local.get().getLast();
    }

    private static void pushStack(obj){
        local.get().addLast(obj);
    }

    private static Object popStack(){
        return local.get().removeLast();
    }

    /** stack end **/

}
